package com.sinosoft.emplmis.labor.web;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.emplmis.application.web.utils.ExcelUtils;
import com.sinosoft.emplmis.labor.entity.LaborInfoEntity;
import com.sinosoft.emplmis.labor.service.LaborInfoService;
import com.sinosoft.emplmis.labor.web.dto.LaborSearchData;
import com.sinosoft.framework.dict.domain.DictCommonEntity;
import com.sinosoft.framework.dict.service.DictService;


@Controller
@RequestMapping("labor/excel")
public class LaborExcelController {
	
	/**
	 * 劳动力信息Service
	 */
	@Autowired
	private LaborInfoService laborInfoService;
	
	@Autowired
	private DictService dictService;
	/*
	 * 导出劳动力信息
	 */
	@RequestMapping(value="/exportLaborInfo")
	@ResponseBody
	public ModelAndView exportLaborInfo(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		
		request.setCharacterEncoding("UTF-8");//设置request的编码方式，防止中文乱码
        String fileName ="城乡劳动力总量情况表(城镇 户籍).xls";//设置导出的文件名称
        String recommendedName = new String(fileName.getBytes(),"iso_8859_1");
        String contentType = "application/vnd.ms-excel";
        String street = request.getParameter("street");
        String community = request.getParameter("community");
        LaborSearchData searchData = new LaborSearchData();
        searchData.setStreet(street);
        searchData.setCommunity(community);
		List<LaborInfoEntity> laborInfoEntityList = laborInfoService.selectAll(searchData);
		// 第一步，创建一个webbook，对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet(fileName);  
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow((int) 0);  
        // 第四步，创建单元格，并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  

        HSSFCell cell = row.createCell(0);  
        cell.setCellValue("姓名");  
        cell.setCellStyle(style);  
      
        cell = row.createCell(1);  
        cell.setCellValue("性别");  
        cell.setCellStyle(style);  
        
        cell = row.createCell(2);  
        cell.setCellValue("身份证号");  
        cell.setCellStyle(style);  
        
        cell = row.createCell(3);  
        cell.setCellValue("学历");  
        cell.setCellStyle(style);
        
        cell = row.createCell(4);  
        cell.setCellValue("联系方式");  
        cell.setCellStyle(style);
        
        cell = row.createCell(5);  
        cell.setCellValue("就业状态");  
        cell.setCellStyle(style);

        cell = row.createCell(6);  
        cell.setCellValue("所在单位");  
        cell.setCellStyle(style);
        
        cell = row.createCell(7);  
        cell.setCellValue("失业时间");  
        cell.setCellStyle(style);
        
        cell = row.createCell(8);  
        cell.setCellValue("街道");  
        cell.setCellStyle(style);
        
        cell = row.createCell(9);  
        cell.setCellValue("社区");  
        cell.setCellStyle(style);
		if(laborInfoEntityList.size() > 0){
			//性别字典
			List<DictCommonEntity> genderDataList = dictService.findSingleDictDetail("dict_gender");
			//学历字典表
			List<DictCommonEntity> educationDataList = dictService.findSingleDictDetail("dict_education");
			//就业状态字典表
			List<DictCommonEntity> employestateDataList = dictService.findSingleDictDetail("dict_employestate");
			//街道字典表
			List<DictCommonEntity> streetDataList = dictService.findSingleDictDetail("dict_street");
			//社区字典表
			List<DictCommonEntity> communityDataList = dictService.findSingleDictDetail("dict_community");
			for(int i = 0; i < laborInfoEntityList.size(); i++){
				row = sheet.createRow((int) i + 1); 
				LaborInfoEntity laborInfo = laborInfoEntityList.get(i);
				 row.createCell(0).setCellValue(laborInfo.getName());
				 //获取性别名称
				 ExcelUtils.getDictNameByCode(row, genderDataList, laborInfo.getSex(),1);
				 row.createCell(2).setCellValue(laborInfo.getIdcard());
				 //获取学历名称
				 ExcelUtils.getDictNameByCode(row, educationDataList, laborInfo.getEducation(),3);
				 row.createCell(4).setCellValue(laborInfo.getPhone());
				 //获取就业状态名称
				 ExcelUtils.getDictNameByCode(row, employestateDataList, laborInfo.getStatus(),5);
				 row.createCell(6).setCellValue(laborInfo.getCompany());
				 row.createCell(7).setCellValue(laborInfo.getUnemploymentdate());
				 //获取街道名称
				 ExcelUtils.getDictNameByCode(row, streetDataList, laborInfo.getStreet(),8);
				 //获取街道名称
				 ExcelUtils.getDictNameByCode(row, communityDataList, laborInfo.getCommunity(),9);
			}
			
		}
		 try  
	        {  
	        	response.setContentType(contentType);
	        	response.setHeader("Content-Disposition", "attachment; filename="+recommendedName);//
	        	OutputStream outputStream = response.getOutputStream();
	            wb.write(outputStream);  
	            outputStream.close();  
	        }  
	        catch (Exception e)  
	        {  
	            e.printStackTrace();  
	        }
		return null;  
	}

	/*
	 * 导出劳动力信息
	 */
	@RequestMapping(value="/exportLoseTouchInfo")
	@ResponseBody
	public String exportLoseTouchInfo(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		
		request.setCharacterEncoding("UTF-8");//设置request的编码方式，防止中文乱码
        String fileName ="失联人员信息统计表.xls";//设置导出的文件名称
        String recommendedName = new String(fileName.getBytes(),"iso_8859_1");
        String contentType = "application/vnd.ms-excel";
        String street = request.getParameter("street");
        String community = request.getParameter("community");
        LaborSearchData searchData = new LaborSearchData();
        searchData.setStreet(street);
        searchData.setCommunity(community);
		List<LaborInfoEntity> laborInfoEntityList = laborInfoService.selectLoseContact(searchData);
		// 第一步，创建一个webbook，对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet(fileName);  
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow((int) 0);  
        // 第四步，创建单元格，并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  

        HSSFCell cell = row.createCell(0);  
        cell.setCellValue("姓名");  
        cell.setCellStyle(style);  
      
        cell = row.createCell(1);  
        cell.setCellValue("性别");  
        cell.setCellStyle(style);  
        
        cell = row.createCell(2);  
        cell.setCellValue("身份证号");  
        cell.setCellStyle(style); 
        
        cell = row.createCell(3);  
        cell.setCellValue("户籍地址");  
        cell.setCellStyle(style);  
        
        cell = row.createCell(4);  
        cell.setCellValue("失联时间");  
        cell.setCellStyle(style);
        
        cell = row.createCell(5);  
        cell.setCellValue("最后联系时间");  
        cell.setCellStyle(style);
        
        cell = row.createCell(6);  
        cell.setCellValue("联系人");  
        cell.setCellStyle(style);

        cell = row.createCell(7);  
        cell.setCellValue("街道");  
        cell.setCellStyle(style);
        
        cell = row.createCell(8);  
        cell.setCellValue("社区");  
        cell.setCellStyle(style);
        
        cell = row.createCell(9);  
        cell.setCellValue("备注");  
        cell.setCellStyle(style);
        if(laborInfoEntityList.size() > 0){
			//性别字典
			List<DictCommonEntity> genderDataList = dictService.findSingleDictDetail("dict_gender");
			//街道字典表
			List<DictCommonEntity> streetDataList = dictService.findSingleDictDetail("dict_street");
			//社区字典表
			List<DictCommonEntity> communityDataList = dictService.findSingleDictDetail("dict_community");
			for(int i = 0; i < laborInfoEntityList.size(); i++){
				row = sheet.createRow((int) i + 1); 
				LaborInfoEntity laborInfo = laborInfoEntityList.get(i);
				 row.createCell(0).setCellValue(laborInfo.getName());
				 //获取性别名称
				 ExcelUtils.getDictNameByCode(row, genderDataList, laborInfo.getSex(),1);
				 row.createCell(2).setCellValue(laborInfo.getIdcard());
				 row.createCell(3).setCellValue(laborInfo.getResidenceAddress());
				 row.createCell(4).setCellValue(laborInfo.getRecentdate());
				 row.createCell(6).setCellValue(laborInfo.getContactperson());
				 //获取街道名称
				 ExcelUtils.getDictNameByCode(row, streetDataList, laborInfo.getStreet(),7);
				 //获取街道名称
				 ExcelUtils.getDictNameByCode(row, communityDataList, laborInfo.getCommunity(),8);
				 row.createCell(9).setCellValue(laborInfo.getRemark());
			}
			
		}
        try  
        {  
        	response.setContentType(contentType);
        	response.setHeader("Content-Disposition", "attachment; filename="+recommendedName);//
        	OutputStream outputStream = response.getOutputStream();
            wb.write(outputStream);  
            outputStream.close();  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }
		return null;
	}
	
}
